module.exports = {
  //请求的url
  url: {
    login: 'http://localhost/auth/wx/login',
    test: "http://localhost/stu/get",
    search: "http://localhost/search/queryGoods",
    guigeListByFid: "http://localhost/guige/listAndVals",
    goodsitem: "http://localhost/goods/queryItemById"
  },
  //发送ajax请求
  ajax: function(param){
    wx.request({
      url: param.url,
      method: param.method ? param.method : 'GET',
      data: param.data,
      header: {
        "content-type": "application/x-www-form-urlencoded",
        token: wx.getStorageSync("login_token")
      },
      success: (data) => {
        //获取服务端返回的数据
        var result = data.data;

        //获取返回的code码
        if(result.code == 200){
          //成功
          if (param.success){
            param.success(data.data.data);
          } 
        } else if(result.code == 411){
          //当前未登录
          wx.showToast({
            title: '当前资源必须登录才能访问',
            icon: 'none',
            duration: 2000
          })
        } else if(result.code == 415){
          //令牌已经过期
          wx.showToast({
            title: '登录凭证已经过期，请重新登录',
            icon: 'none',
            duration: 2000
          });

          //删除本地过期的令牌
          wx.removeStorageSync("login_token");

          //调用开发者自定义的过期方法
          if(param.expire){
            param.expire(result);
          }
        }
      }
    })
  }  
}
